#include "stdafx.h"
#include "../client/DCPlusPlus.h"
#include "KeywordDlg.h"
#include "../client/SyntaxManager.h"

#define ATTACH(id, var) var.Attach(GetDlgItem(id))
#define BUFLEN 32

LRESULT KeywordDlg::OnInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/) {
	
	ATTACH(IDC_EDIT_KEYWORD, ctrlKeyword);
	ATTACH(IDC_ADD, ctrlAddKeyword);
	ATTACH(IDC_CHANGE, ctrlChangeKeyword);
	ATTACH(IDC_REMOVE, ctrlRemoveKeyword);
	ATTACH(IDC_KEYWORDS, ctrlKeywordList);
	ATTACH(IDC_KEYWORD_TYPE, ctrlType);

	ctrlType.AddString(CTSTRING(KEYWORDDLG_KEYWORD));
	ctrlType.AddString(CTSTRING(KEYWORDDLG_LIBRARY));
	ctrlType.AddString(CTSTRING(KEYWORDDLG_OPERATOR));
	curSel = _KEYWORD;
	ctrlType.SetCurSel(curSel);

	CRect rc;
	ctrlKeywordList.GetClientRect(rc);
	ctrlKeywordList.InsertColumn(0, _T(""), LVCFMT_LEFT, rc.Width(), 0);
	ctrlKeywordList.SetExtendedListViewStyle(LVS_EX_FULLROWSELECT);
	ctrlKeywordList.setSort(0, ExListViewCtrl::SORT_STRING_NOCASE);

	Keywords::List& kl = SyntaxManager::getInstance()->getKeywords();
	for(Keywords::Iter i = kl.begin(); i != kl.end(); ++i) {
		switch(i->getType()) {
			case _KEYWORD: kList.push_back(i->getKeyword()); break;
			case _LIBRARY: lList.push_back(i->getKeyword()); break;
			case _OPERATOR: oList.push_back(i->getKeyword()); break;
			default: dcassert(0);
		}
	}
	for(StringIter j = kList.begin(); j != kList.end(); ++j) {
		ctrlKeywordList.insert(ctrlKeywordList.GetItemCount(), *j);
	}
	
	CenterWindow(GetParent());
	return FALSE;
}

LRESULT KeywordDlg::OnCloseCmd(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/) {
	if(wID == IDOK) {
		updateList();
		Keywords::List& kl = SyntaxManager::getInstance()->getKeywords();
		kl.clear();
		for(StringIter i = kList.begin(); i != kList.end(); ++i) {
			kl.push_back(Keywords(*i, _KEYWORD));
		}
		for(StringIter j = lList.begin(); j != lList.end(); ++j) {
			kl.push_back(Keywords(*j, _LIBRARY));
		}
		for(StringIter k = oList.begin(); k != oList.end(); ++k) {
			kl.push_back(Keywords(*k, _OPERATOR));
		}
		SyntaxManager::getInstance()->saveKeywords();
	}
	ctrlKeywordList.DeleteAllItems();
	ctrlKeywordList.Detach();
	EndDialog(wID);
	return 0;
}

LRESULT KeywordDlg::onAdd(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/) {
	if(ctrlKeyword.GetWindowTextLength() > 0) {
		if(curSel == _OPERATOR && ctrlKeyword.GetWindowTextLength() > 1) { return 0; }
		TCHAR buf[BUFLEN];
		ctrlKeyword.GetWindowText(buf, BUFLEN);
		ctrlKeywordList.insert(ctrlKeywordList.GetItemCount(), buf);
		ctrlKeyword.SetWindowText(_T(""));
	}
	return 0;
}
LRESULT KeywordDlg::onChange(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/) {
	if(ctrlKeyword.GetWindowTextLength() > 0 && ctrlKeywordList.GetSelectedCount() == 1) {
		TCHAR buf[BUFLEN];
		ctrlKeyword.GetWindowText(buf, BUFLEN);
		ctrlKeywordList.SetItemText(ctrlKeywordList.GetNextItem(-1, LVNI_SELECTED), 0, buf);
		ctrlKeyword.SetWindowText(_T(""));
	}
	return 0;
}
LRESULT KeywordDlg::onRemove(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/) {
	if(ctrlKeywordList.GetSelectedCount() == 1) {
		ctrlKeywordList.DeleteItem(ctrlKeywordList.GetNextItem(-1, LVNI_SELECTED));
	}
	return 0;
}

void KeywordDlg::updateList() {
	int it = ctrlKeywordList.GetItemCount();
	TCHAR buf[BUFLEN];
	switch(curSel) {
		case _KEYWORD: 
			{
				kList.clear();
				for(int i = 0; i < it; ++i) {
					ctrlKeywordList.GetItemText(i, 0, buf, BUFLEN);
					kList.push_back(buf);
				}
			}
			break;
		case _LIBRARY: 
			{
				lList.clear();
				for(int j = 0; j < it; ++j) {
					ctrlKeywordList.GetItemText(j, 0, buf, BUFLEN);
					lList.push_back(buf);
				}
			}
			break;
		case _OPERATOR: 
			{
				oList.clear();
				for(int k = 0; k < it; ++k) {
					ctrlKeywordList.GetItemText(k, 0, buf, BUFLEN);
					oList.push_back(buf);
				}
			}
			break;
		default: dcassert(0);
	}
	curSel = ctrlType.GetCurSel();
	ctrlKeywordList.DeleteAllItems();
	switch(curSel) {
		case _KEYWORD: 
			for(StringIter i = kList.begin(); i != kList.end(); ++i) {
				ctrlKeywordList.insert(ctrlKeywordList.GetItemCount(), *i);
			}
			break;
		case _LIBRARY: 
			for(StringIter j = lList.begin(); j != lList.end(); ++j) {
				ctrlKeywordList.insert(ctrlKeywordList.GetItemCount(), *j);
			}
			break;
		case _OPERATOR: 
			for(StringIter k = oList.begin(); k != oList.end(); ++k) {
				ctrlKeywordList.insert(ctrlKeywordList.GetItemCount(), *k);
			}
			break;
		default: dcassert(0);
	}
	ctrlKeywordList.resort();
}